﻿#region Copyright © 2013 Paul Miller (Millz)
// All rights are reserved. Reproduction or transmission in whole or in part,
// any form or by any means, electronic, mechanical or otherwise, is prohibited
// without the prior written consent of the copyright owner.
// 
// Filename: Insanity.cs
// Date:     23/09/2013
// Author:   Paul Miller (Millz)
#endregion

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Insanity.GUI;
using Insanity.Helpers;
using Insanity.Core;
using Insanity.Managers;
using Styx;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.Helpers;
using Styx.TreeSharp;
using Styx.WoWInternals.WoWObjects;

namespace Insanity
{
    public partial class Insanity : CombatRoutine
    {
        public override sealed string Name { get { return "Insanity Free [Millz]"; } }
        public override WoWClass Class { get { return Me.Specialization == WoWSpec.PriestShadow ? WoWClass.Priest : WoWClass.None; } }
        private static LocalPlayer Me { get { return StyxWoW.Me; } }
        private static Insanity Instance { get; set; }
        private Form _InsanityGui;
        internal event OnPulseHandler PulseEvent;
        internal delegate void OnPulseHandler();

        public override void Initialize()
        {
            try
            {
            
            TalentManager.Update(); // Update talents
            
            RoutineManager.Reloaded += (s, e) =>
            {
                Log.Debug("Creating Behaviors");
                RebuildBehaviors();
            };

            // Init Behaviours
            if (_combatBehavior == null)
                _combatBehavior = new PrioritySelector();

            if (_preCombatBuffBehavior == null)
                _preCombatBuffBehavior = new PrioritySelector();

            if (!RebuildBehaviors())
            {
                return;
            }

            Log.Debug("Behaviors Created");
            // PRINT OUT CLIENT LATENCY
            Log.Debug("Client Latency: {0}", StyxWoW.WoWClient.Latency);

            // PRINT OUT LAST RESTART TIME
            Log.Debug("{0:F1} days since Windows was started.", TimeSpan.FromMilliseconds(Environment.TickCount).TotalHours / 24.0);

            // PRINT OUT SETTINGS
            Settings.InsanitySettings.LogSettings();

            // Get the latest version from the change log.
            var latestVersion = ConfigurationForm.ChangeLogText.Split('\n')[0];

            Log.Info("Thanks for choosing Insanity Free! [Update: {0}", latestVersion.Replace("[",""));
            Log.Info("You are a level {0} {1} ({2})", Me.Level, Me.Race, Me.IsAlliance ? "For the Alliance!" : "For the Horde!");

                if (!GlobalSettings.Instance.UseFrameLock)
                {
                    Log.Red("Framelock is NOT active! Please enable within the Settings & Tools dialog.");
                }

            Log.Red("Please note: Insanity Free does not support AFK bot bases, such as Questing, BGBuddy, Gatherbuddy etc.");

                
            }
            catch (Exception ex)
            {
                Log.Debug("Exception thrown at Initialize: {0}", ex.ToString());
            }
        }

        public Insanity()
        {
            Instance = this;

        }

        public override void Pulse()
        {
            try
            {
                Spell.PulseDoubleCastEntries();
                OnPulseHandler handler = PulseEvent;

                if (handler != null)
                    handler();

            }
            catch (Exception ex)
            {
                
                Log.Debug("Exception in Pulse: {0}", ex);
            }
            
        }

        #region Extensions/Utilities/Auth

        private static void StopBot(string reason)
        {
            if (TreeRoot.IsRunning)
            {
                Log.Debug(reason);
                TreeRoot.Stop();
            }
                
        }

        public override bool WantButton
        {
            get { return true; }
        }


        public override void OnButtonPress()
        {
            if (_InsanityGui == null || _InsanityGui.IsDisposed || _InsanityGui.Disposing) _InsanityGui = new ConfigurationForm();
            
            if (_InsanityGui != null || _InsanityGui.IsDisposed) _InsanityGui.ShowDialog();
        }

        #endregion

    }
}
